﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace Student
{
    public partial class Group_setting : Form
    {
        BLL.B_Group bllGroup = new BLL.B_Group();
        BLL.B_Student b_Student = new BLL.B_Student();
        List<Models.Student> all_stu_wo_group;
        List<Models.Student> group_members;
        StuGroup stu_Group;


        public Group_setting()
        {
            InitializeComponent();
        }

        private void join_Group_Click(object sender, EventArgs e)
        {
            //判时
            if (LogInfo.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            if (LogInfo.student.is_gleader==0)
            {
                MessageBox.Show("组长才能选择组员！");
                return;
            }    

            List<Models.Student> group_stu = new List<Models.Student>();
            foreach (DataGridViewRow dr in Student_list.SelectedRows)
            {
                group_stu.Add(all_stu_wo_group[dr.Index]);
            }
            //
            if (group_stu.Count + stu_Group.members > LogInfo.Sys_Config.group_members)
            {
                MessageBox.Show("队伍人数超上限！");
                return;
            }

            if(!bllGroup.AddStudent(group_stu,stu_Group))
            {
                MessageBox.Show("添加失败！");
                return;
            }
            group_members = b_Student.GetList(stu_Group);
            Group_members.DataSource = group_members;
            all_stu_wo_group = b_Student.GetListNotSetGroup();
            Student_list.DataSource = all_stu_wo_group;
        }


        public static T CloneObject<T>(T obj) where T : class
        {
            //反射
            Type type = typeof(T);

            // 创建新对象
            T clone = Activator.CreateInstance(type) as T;

            // 获取所有字段
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            // 复制字段的值
            foreach (FieldInfo field in fields)
            {
                field.SetValue(clone, field.GetValue(obj));
            }

            // 获取所有属性
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            // 复制属性的值
            foreach (PropertyInfo property in properties)
            {
                if (property.CanWrite)
                {
                    property.SetValue(clone, property.GetValue(obj));
                }
            }

            return clone;//return class
        }
        private void commit_Group_Click(object sender, EventArgs e)
        {
            if(LogInfo.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            if (stu_Group == null)
            {
                stu_Group = new Models.StuGroup();
            }
            stu_Group.group_name = Group_name.Text;
            stu_Group.project_name = Project_name.Text;
            stu_Group.project_info = Group_info.Text;

            if (Group_name.Text == "" || Project_name.Text == "" || Group_info.Text == "")
            {
             
                MessageBox.Show("请填写完整信息");
                return;
            }

            BLL.B_Group b_Group = new BLL.B_Group();
            try
            {
                Models.Student student = CloneObject(Models.LogInfo.student);
                if (b_Group.Commit(stu_Group, student))
                {
                    MessageBox.Show("ok!");
                    LogInfo.student = student;
                }
                else
                    MessageBox.Show("更新失败，请确定是否为队长或者数据库问题？");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            all_stu_wo_group = b_Student.GetListNotSetGroup();
            Student_list.DataSource = all_stu_wo_group;

            group_members = b_Student.GetList(stu_Group);
            Group_members.DataSource = new BindingList<Models.Student>(group_members);
        }

        private void Group_Load(object sender, EventArgs e)
        {
            all_stu_wo_group = b_Student.GetListNotSetGroup();
            Student_list.DataSource = all_stu_wo_group;

            stu_Group = bllGroup.GetGroup(LogInfo.student);
            if(stu_Group != null )
            {
                Group_name.Text = stu_Group.group_name;
                Project_name.Text = stu_Group.project_name;
                Group_info.Text = stu_Group.project_info;

                group_members = b_Student.GetList(stu_Group);
                Group_members.DataSource = new BindingList<Models.Student>(group_members);
            }
        }

        private void release_Click(object sender, EventArgs e)
        {
            if (LogInfo.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            if (LogInfo.student.is_gleader == 0)
            {
                MessageBox.Show("组长才能解散队伍！");
                return;
            }

            BLL.B_Group b_Group = new BLL.B_Group();
            b_Group.Release(stu_Group);

            clear_control();
            Group_members.DataSource = null;
            all_stu_wo_group = b_Student.GetListNotSetGroup();
            Student_list.DataSource = all_stu_wo_group;
        }

        private void clear_control()
        {
            Group_name.Text = "";
            Project_name.Text = "";
            Group_info.Text = "";
        }

        private void Remove_stu_Click(object sender, EventArgs e)
        {
            if (LogInfo.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            if (LogInfo.student.is_gleader == 0)
            {
                MessageBox.Show("组长才能编辑组员！");
                return;
            }
            
            List<Models.Student> Removed_stu = new List<Models.Student>();
            Models.Student stu;

            foreach (DataGridViewRow dr in Group_members.SelectedRows)
            {
                stu = group_members[dr.Index];
                if(stu.stu_id == LogInfo.student.stu_id) 
                { break; }
                Removed_stu.Add(stu);
            }

            if (!bllGroup.RemoveStudent(Removed_stu, stu_Group))
            {
                MessageBox.Show("删除失败！");
                return;
            }
            group_members = b_Student.GetList(stu_Group);
            Group_members.DataSource = group_members;
            all_stu_wo_group = b_Student.GetListNotSetGroup();
            Student_list.DataSource = all_stu_wo_group;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
